var costRules = new Object;
var indentPrice = new Number(0);
$(function() {
	// 手机号码验证
	jQuery.validator
			.addMethod(
					"isMobile",
					function(value, element) {
						var length = value.length;
						var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
						var re = /^0\d{2,3}-?\d{7,8}$/;
						return this.optional(element)
								|| (length == 11 && mobile.test(value))
								|| re.test(value);
					}, "请正确填写您的电话号码");
	validateAddRepairIndentForm();
	$("input[name='addPrice']").TouchSpin({
		min : 0,
		max : 1000,
		//step : 5,
		boostat : 5,
		maxboostedstep : 10,
		postfix : '元'
	});
	$(".input_accText").TouchSpin({
		min : 0,
		max : 100,
		step : 1,
		boostat : 5,
		maxboostedstep : 10,
	});
	$.post("../territory/selectAvailableProvince", function(data) {
		$("#Province>option").first().nextAll().remove();
		$("#pickupProvince>option").first().nextAll().remove();
		// 这是所有的省份
		$.each(data, function(i, option) {
			$("#Province").append(
					'<option value="' + option.code + '">' + option.province
							+ '</option>');
			$("#pickupProvince").append(
					'<option value="' + option.code + '">' + option.province
							+ '</option>');
		});
	});
});
// 打开和关闭添加取货地址DIV
function changeNeedPickUpForIndent(radio) {
	if (radio.value == 0) {
		$("#pickupDetailDIV").hide();
	} else {
		$("#pickupDetailDIV").show();
	}
}

function loadAccessoryInputTouchSpin(cityCode) {
	if (cityCode.indexOf("999999") > 0 || cityCode.indexOf("999999") == 0) {
		$("#input_needPickupPrice").val("");
	} else {
		$.post("../cityCostRule/queryCostRuleByCityCode", {
			cityCode : cityCode
		}, function(data) {
			costRules = data;
			$("#input_needPickupPrice").val(data.takeGoodsPrice + "元");
		});
	}
}
$("#City").change(
		function() {
			$("#countrySelect>option").first().nextAll().remove();
			if (($("#City").val()).indexOf("999999") < 0) {
				$.post("../territory/selectAvailableCountry", {
					cityCode : $("#City").val()
				}, function(data) {
					$.each(data, function(i, option) {
						$("#countrySelect").append(
								'<option value="' + option.code + '">'
										+ option.country + '</option>');
					});
					$("#countrySelect>option:nth-child(2)").attr("selected",
							"selected"); // 给县默认赋值为第一个；
				});
			}
			loadAccessoryInputTouchSpin($("#Province").val() + "/"
					+ $("#City").val());
		});
$("#Province").change(
		function() {
			$("#City>option").first().nextAll().remove();
			$("#countrySelect>option").first().nextAll().remove();
			if (($("#Province").val()).indexOf("999999") < 0) {
				$.post("../territory/selectAvailableCity", {
					provinceCode : $("#Province :selected").val()
				},
						function(data) {
							$.each(data, function(i, option) {
								$("#City").append(
										'<option value="' + option.code + '">'
												+ option.city + '</option>');
							});
							$("#City>option:nth-child(2)").attr("selected",
									"selected"); // 给市默认赋值为第一个；
							var city = $("#City :selected").val();
							var cityCodeStr = $("#Province :selected").val()
									+ "/" + city;
							loadAccessoryInputTouchSpin(cityCodeStr);
							$.post("../territory/selectAvailableCountry", {
								cityCode : city
							}, function(data) {
								if (data && data.length > 0) {
									$.each(data, function(i, option) {
										$("#countrySelect").append(
												'<option value="' + option.code
														+ '">' + option.country
														+ '</option>');
									});
									$("#countrySelect>option:nth-child(2)")
											.attr("selected", "selected"); // 给县默认赋值为第一个；
								}
							});
						});
			} else {
				$("#input_needPickupPrice").val("");
			}
		});
$("#pickupCity").change(
		function() {
			$("#pickupCountrySelect>option").first().nextAll().remove();
			$.post("../territory/selectAvailableCountry", {
				cityCode : $("#pickupCity").val()
			}, function(data) {
				$.each(data, function(i, option) {
					$("#pickupCountrySelect").append(
							'<option value="' + option.code + '">'
									+ option.country + '</option>');
					$("#pickupCountrySelect>option:nth-child(2)").attr(
							"selected", "selected"); // 给县默认赋值为第一个；
				});
			});
		});
$("#pickupProvince").change(
		function() {
			$("#pickupCity>option").first().nextAll().remove();
			$("#pickupCountrySelect>option").first().nextAll().remove();
			$.post("../territory/selectAvailableCity", {
				provinceCode : $("#pickupProvince").val()
			}, function(data) {
				$.each(data, function(i, option) {
					$("#pickupCity").append(
							'<option value="' + option.code + '">'
									+ option.city + '</option>');
					$("#pickupCity>option:nth-child(2)").attr("selected",
							"selected"); // 给市默认赋值为第一个；
					$.post("../territory/selectAvailableCountry", {
						cityCode : $("#pickupCity").val()
					}, function(data) {
						if (data && data.length > 0) {
							$.each(data,
									function(i, option) {
										$("#pickupCountrySelect").append(
												'<option value="' + option.code
														+ '">' + option.country
														+ '</option>');
									});
							$("#pickupCountrySelect>option:nth-child(2)").attr(
									"selected", "selected"); // 给县默认赋值为第一个；
						}
					});

				});
			});
		});
// 校验添加维修订单
function validateAddRepairIndentForm() {
	$("#addRepairIndentForm").validate({
		rules : {
			name : "required",
			phone : {
				required : true,
				isMobile : true
			},
			subscribeTime : {
				required : true,
			},
			weiXbody : {
				required : true,
				minlength : 2
			},
			province : {
				max : 999998
			},
			city : {
				max : 999998
			},
			country : {
				max : 999998
			},
			repairAddress : {
				minlength : 2
			},
			pickupPhone : {
				required : true,
				isMobile : true
			},
			pickupProvince : {
				max : 999998
			},
			pickupCity : {
				max : 999998
			},
			pickupCountry : {
				max : 999998
			},
			pickAddress : {
				required : true,
				minlength : 2
			}
		},
		errorClass : "validateFontSize error",
		messages : {
			name : {
				required : "请输入顾客姓名"
			},
			phone : {
				required : "请输入电话",
				isMobile : "请正确填写您的电话号码"
			},
			subscribeTime : {
				required : "请输入维修时间",
			},
			weiXbody : {
				required : "请填写维修信息",
				minlength : "维修信息长度至少为10"
			},
			province : {
				max : "省未填"
			},
			city : {
				max : "市未填"
			},
			country : {
				max : "区未填"
			},
			repairAddress : {
				required : "请输入详细地址",
				minlength : "详细地址长度至少为2"
			},
			pickupPhone : {
				required : "请输入手机号",
				isMobile : "请正确填写手机号码"
			},
			pickupProvince : {
				max : "省未填"
			},
			pickupCity : {
				max : "市未填"
			},
			pickupCountry : {
				max : "区未填"
			},
			pickAddress : {
				required : "请输入详细地址",
				minlength : "详细地址长度至少为5"
			}
		}
	});
}

function makeRepairIndent(object) {
	if ($("#addRepairIndentForm").valid()) {
		$(object).removeClass('btn-info');
		$(object).addClass('btn-warning');
		$(object).html("生成中...");
		$(object).attr('disabled', 'disabled');
		$.post("../indent/makeRepairIndent", $("#addRepairIndentForm")
				.serialize(), function(data) {
			if (data.status == 0) {
				$("input[name='indentID']").val(data.data);
				$(object).removeClass('btn-warning');
				$(object).addClass('btn-info');
				$(object).html("生成订单");
				countIndentPrice();
				$(object).removeAttr('disabled');
				$("#addRepairIndentForm")[0].reset();
				$("#City>option:nth-child(1)").attr("selected", "selected"); // 给县默认赋值为第一个；
				$("#countrySelect>option:nth-child(1)").attr("selected",
						"selected"); // 给县默认赋值为第一个；
				$("#txt_crampetPrice").html(
						"壁钩安装费" + costRules.crampetPrice + "元/对");
				$("#txt_fourClawPrice").html(
						"四抓钩安装费" + costRules.fourClawPrice + "元/套");
				$("#txt_motorPrice").html(
						"电机安装费" + costRules.motorPrice + "元/台");
				$("#accesstorySelectDiv").show();
				$("#payIndentModal").modal('show');
			}
		});
	}
}
function payRepairIndent(url) {
	$("#payIndentForm").attr("action",url);
	$("#payIndentForm").submit();
}
//////////////////余额支付-START///////////////////////////////
//////////////////余额支付-END///////////////////////////////
// 关闭付款窗口
function closePayIndentModal() {
	Ewin.confirm({
		message : "生成的订单可以在我的未付款订单中查询和付款"
	}).on(function(e) {
		if (!e) {
			window.location.reload();
			return;
		} else {
			window.location.href = '../indent/repairList/pay.htm'
		}
	})
}
// 预览订单的价格
$("#payIndentForm input").change(function() {
	countTotalIndentPrice();
});
// 计算订单的总价格
function countTotalIndentPrice() {
	if (typeof costRules.crampetPrice != "undefined") {
		var totalIndentPrice = new Number(indentPrice);
		// 选择数量的价钱
		var crampetNumber = $("#payIndentForm input[name='crampetNumber']")
				.val();
		var fourClawNumber = $("#payIndentForm input[name='fourClawNumber']")
				.val();
		var motorNumber = $("#payIndentForm input[name='motorNumber']").val();
		var accPrice = parseInt(crampetNumber)
				* parseInt(costRules.crampetPrice);
		accPrice = accPrice + parseInt(fourClawNumber)
				* parseInt(costRules.fourClawPrice);
		accPrice = accPrice + parseInt(motorNumber)
				* parseInt(costRules.motorPrice);
		totalIndentPrice = totalIndentPrice + accPrice
				+ parseInt($("#payIndentForm input[name='addPrice']").val());
		if ($("#payIndentForm input[name='relievedBao']:checked").val() == 1) {
			totalIndentPrice = totalIndentPrice + 5;
		}
		$("#payIndentForm #totalPrice").val(totalIndentPrice);
	}
}
// 计算订单的价格
function countIndentPrice() {
	var _indentPrice = new Number(0);
	_indentPrice = parseInt(costRules.repairStartingFare);
	if ($("input[name='isNeedTakeGoods']:checked").val() == 1) {
		_indentPrice = _indentPrice + parseInt(costRules.takeGoodsPrice);
	}
	indentPrice = _indentPrice;
	$("#payIndentForm #totalPrice").val(indentPrice + 5);
}